﻿#define EnableAll
using System;
using System.Net;
using System.Windows.Forms;
using Conusic;
using Conusic.Model.Audio.Server;
using MusicPage;

namespace ConusicUI
{
    /// <summary>
    /// Class with program entry point.
    /// </summary>
    internal static class Program
    {
        private static ConnectionTester internetConnectionWindow;

        private static void SimpleHttp_OnError(Exception e)
        {
            // Ignore if another window configuration open
            if (internetConnectionWindow == null)
            {
                internetConnectionWindow = new ConnectionTester(e);
                if (Form.ActiveForm != null)
                    Form.ActiveForm.Invoke(new MethodInvoker(
                                               delegate { internetConnectionWindow.ShowDialog(Form.ActiveForm); }));
                else
                {
                    internetConnectionWindow.ShowDialog();
                }

                internetConnectionWindow.Dispose();
                internetConnectionWindow = null;
            }
        }

        private static void RunApplication()
        {
            Application.Run(new Main());
        }

        /// <summary>
        /// Program entry point.
        /// </summary>
        [STAThread]
        private static void Main( /*string[] args*/)
        {
            // Show message box with unhandled erros descriptions
            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Subscribe to event error handler in http requests
            SimpleHttp.OnError += SimpleHttp_OnError;
#if EnableAll
            // Run audio server in background mode
            AudioServer audioServer = new AudioServer(Conusic.Properties.Settings.Default.AudioServerPort);


            // Safe program from http 417 error - do not send except header in webrequest
            // http://blogs.msdn.com/b/shitals/archive/2008/12/27/9254245.aspx
            ServicePointManager.Expect100Continue = false;

            AutoUpdate.CheckUpdateIfEnabled();

            #region Check authorization and run program

            bool successAuthorization = false;
            successAuthorization = RunAuthorization();

            if (successAuthorization)
            {
#if DEBUG
                var s = new QuickListen();
                Application.Run(s);

#endif
#if !DEBUG
                RunApplication();
#endif
            }

            #endregion

            Conusic.Properties.Settings.Default.Save();
            ConusicUI.Properties.Settings.Default.Save();
#endif
            //Application.Exit();
        }

        private static bool RunAuthorization()
        {
            if (!String.IsNullOrEmpty(Conusic.Properties.Settings.Default.SID) &&
                VkontakteAuthorization.CheckAuthorization(Conusic.Properties.Settings.Default.SID))
            {
                return true;
            }

            var authForm = new FormAuth();
            if (authForm.ShowDialog() == DialogResult.OK)
            {
                return VkontakteAuthorization.CheckAuthorization(Conusic.Properties.Settings.Default.SID);
            }
            else
            {
                return false;
            }
        }

        public static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs eventArgs)
        {
            var messaage = sender + "\n\n" + eventArgs.ExceptionObject;
            MessageBox.Show(null, messaage, @"Unhandled exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}